package com.javalong.affairs.module.renwujinzhang.activity.receive;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.EditText;

import com.google.gson.reflect.TypeToken;
import com.javalong.affairs.R;
import com.javalong.affairs.base.common.ISynCallback;
import com.javalong.affairs.base.common.UIStatus;
import com.javalong.affairs.base.common.ViewHolder;
import com.javalong.affairs.base.constant.BundleKey;
import com.javalong.affairs.base.constant.RequestCode;
import com.javalong.affairs.base.constant.RequestKey;
import com.javalong.affairs.base.listener.BaseResponseListener;
import com.javalong.affairs.base.manager.IntentManager;
import com.javalong.affairs.base.model.User;
import com.javalong.affairs.base.utils.HttpUtils;
import com.javalong.affairs.base.utils.IntUtils;
import com.javalong.affairs.base.utils.StringUtils;
import com.javalong.affairs.base.utils.TimeUtils;
import com.javalong.affairs.base.utils.ToastUtils;
import com.javalong.affairs.base.utils.UrlUtils;
import com.javalong.affairs.module.notifything.activity.NewReceiverActivity;
import com.javalong.affairs.module.renwujinzhang.activity.AuditoerActivity;
import com.javalong.affairs.module.renwujinzhang.activity.NewNodeActivity;
import com.javalong.affairs.module.renwujinzhang.activity.RecycleListActivity;
import com.javalong.affairs.module.renwujinzhang.activity.SupervisesActivity;
import com.javalong.affairs.module.renwujinzhang.activity.WatchRunTaskActivity;
import com.javalong.affairs.module.renwujinzhang.activity.progress.ProgressActivity;
import com.javalong.affairs.module.renwujinzhang.activity.submit.TaskSubmitActivity;
import com.javalong.affairs.module.renwujinzhang.activity.suggest.activity.SuggestListActivity;
import com.javalong.affairs.module.renwujinzhang.model.ProgressModel;
import com.javalong.affairs.module.renwujinzhang.model.Task;
import com.javalong.affairs.module.renwujinzhang.ui.SelectTimeView;
import com.javalong.affairs.module.renwujinzhang.ui.SelectUserView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 接收任务中 的进行中任务
 */
public class WatchRunReceiveTaskActivity extends WatchRunTaskActivity implements ISynCallback {


    public static void toThisActivity(Context context, Task task) {
        Intent intent = new Intent();
        intent.setClass(context, WatchRunReceiveTaskActivity.class);
        intent.putExtra(BundleKey.TASK, task);
        context.startActivity(intent);
    }


    @Override
    public void afterInit() {
        super.afterInit();
        uiHandler.registerCallback(this, flag);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_task_receive_run, menu);
        return true;
    }

    private int flag = IntUtils.getNextInt();

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_assign:
                if (task.getChangeNext() == 0) {
                    ToastUtils.showMessage("任务已下发");
                } else {
                    NewNodeActivity.toThisActivity(this, task);
                }
                break;
            case R.id.action_submit:
                TaskSubmitActivity.toThisActivity(this, task);
                break;
            case R.id.action_supervise:
                SupervisesActivity.toThisActivity(WatchRunReceiveTaskActivity.this, task.getId());
                break;
            case R.id.action_audit:
                AuditoerActivity.toThisActivity(WatchRunReceiveTaskActivity.this, task.getId());
                break;
            case R.id.action_suggest:
                SuggestListActivity.toThisActivity(WatchRunReceiveTaskActivity.this, task.getId());
                break;
            case R.id.action_work_report: {
                Bundle bundle = new Bundle();
                bundle.putInt("type", 0);
                bundle.putInt("flag", flag);
                IntentManager.doActivityForResult(this,
                        NewReceiverActivity.class, RequestCode.RECEIVER, bundle);
            }
            break;
            case R.id.action_cooperation: {
                Bundle bundle = new Bundle();
                bundle.putInt("type", 0);
                bundle.putInt("flag", flag);
                IntentManager.doActivityForResult(this,
                        NewReceiverActivity.class, RequestCode.RECEIVER, bundle);
            }
            break;
            default:
                super.onOptionsItemSelected(item);
                break;
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        uiHandler.unRegisterCallback(this, flag);
    }

    @Override
    public void OnSynCalled(int ulID, Bundle bundle) {
        if (flag == ulID) {
            List<User> userList = (List<User>) bundle.getSerializable(BundleKey.USERLIST);
            String url = UrlUtils.getUrl("Task", "action_superviseAdd");
            Map<String, Object> params = new HashMap<String, Object>();
            params.put(RequestKey.ID, task.getId());
            params.put(RequestKey.SUPERVISES, StringUtils.getUserIds(userList));
            HttpUtils.post(url, params, new BaseResponseListener(null) {
                @Override
                public void onSuccess(Object obj) {
                    super.onSuccess(obj);
                }
            });
        }
    }
}
